﻿@{
    // Set the layout page and page title
    Layout = "~/tpl/_layout.cshtml";
    PageData["PageTitle"] = "Forget Your Password?";

    bool isValid = true;
    bool isSuccess = false;
    var errors = new Dictionary<string,string>();
    var currentPassword = Request["currentPassword"];
    var newPassword = Request["newPassword"];
    var confirmPassword = Request["confirmPassword"];

    if (IsPost) 
    {
        if (currentPassword.IsEmpty()) 
        {
            errors.Add("currentPasswordError", "Please enter your current password.");
            isValid = false;
        }
        if (newPassword.IsEmpty()) 
        {
            errors.Add("newPasswordError", "Please enter a new password.");
            isValid = false;
        }
        
        if (confirmPassword.IsEmpty()) 
        {
            errors.Add("confirmPasswordError", "Please confirm your new password.");
            isValid = false;
        }
        else if(confirmPassword != newPassword) 
        {
            errors.Add("confirmPasswordError", "The password confirmation does not match the new password.");
            isValid = false;
        }

        if(isValid) 
        {
            if(WebSecurity.ChangePassword(WebSecurity.CurrentUserName, currentPassword, newPassword)) 
            {
                isSuccess = true;
            }
            else 
            {
                errors.Add("errorMessage", "An error occurred when attempting to change the password. Please contact the site owner.");
            }
        }
        else 
        {
            errors.Add("errorMessage", "Password change failed. Please correct the errors and try again.");
        }
    }
}

<fieldset>
    <legend>Change Password</legend>
    <p>
        Use this form to change your password. You'll be required to enter your current password. 
        Click <a href="ForgotPassword">here</a> if you've forgotten your password.
    </p>
    <form method="post" action="">
        @if (isSuccess) 
        {
            <div>
                Your password has been updated!
            </div>
        }
        @if (errors.ContainsKey("errorMessage")) 
        {
            <div>
                @errors["errorMessage"];
            </div>
        }
        <div>
            <label for="currentPassword">Current Password</label>
            <input type="password" name="currentPassword" value="" />
            @if (errors.ContainsKey("currentPasswordError"))
            {
                <span>@errors["currentPasswordError"]</span>
            }
        </div>
        <div>
            <label for="newPassword">New Password:</label> 
            <input type="password" name="newPassword" />
            @if (errors.ContainsKey("newPasswordError")) 
            {
                <span>@errors["newPasswordError"]</span>
            }
        </div>
        <div>
            <label for="confirmPassword">Confirm Password:</label> 
            <input type="password" name="confirmPassword" />
            @if (errors.ContainsKey("confirmPasswordError"))
            {
                <span>@errors["confirmPasswordError"]</span>
            }
        </div>

        <div>
            <input type="submit" value="Change Password" />
        </div>
    </form>
</fieldset>
